//
//  VerifyPhoneViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/7.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "VerifyPhoneViewModel.h"
#import "VerifyPhoneView.h"
#import "VerificationHelper.h"

@interface VerifyPhoneViewModel ()<UITextFieldDelegate>
{
    NSString *tipStr;
}
@end

@implementation VerifyPhoneViewModel

- (instancetype)initWithSuperView:(UIView *)superView
{
    if (self = [super initWithSuperView:superView]) {
        VerifyPhoneView *view = [[VerifyPhoneView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - kHeightProportion_In_iPhoneTriploid(64)) inSuperView:superView];
        if (iPhoneX) {
            view.height = KScreenHeight - kHeightProportion_In_iPhoneTriploid(34) - kHeightProportion_In_iPhoneTriploid(88);
        }
        view.textFild.delegate = self;
        [view.rightBtn addTarget:self action:@selector(rightBtnA:) forControlEvents:UIControlEventTouchUpInside];
        [view.withDrawBtn addTarget:self action:@selector(withDrawBtnA:) forControlEvents:UIControlEventTouchUpInside];

    }
    return self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *str = [NSString stringWithTextFiledStr:textField.text replacementString:string];
    tipStr = str;
    return YES;
}

- (void)rightBtnA:(GLGWButton *)sender
{
    sender.enabled = NO;
//    [SVProgressHUD showWithStatus:@"发送验证码中..."];
//    [CYSMSTool getRegisterSMSCodeWithMobile:_phoneNumberTF.text finish:^(BOOL success, id error) {
//        [SVProgressHUD dismiss];
//        if (success) {
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
////                [SVProgressHUD showInfoWithStatus:@"验证码已发送"];
//            });
//        }
        sender.enabled = YES;
        BOOL isExist = YES;
        if (isExist) {
            sender.userInteractionEnabled = NO;
            [VerificationHelper timerCountDown:48 completionBlock:^{
                sender.userInteractionEnabled = YES;
                [sender setTitle:@"发送验证码" forState:UIControlStateNormal];
            } isRunBlock:^(NSString *countDownSec) {
                [sender setTitle:countDownSec forState:UIControlStateNormal];
            }];
        }
        
//    }];
}

- (void)withDrawBtnA:(GLGWButton *)sender
{
    if (tipStr.length <= 0) {
        return;
    }
    
    if (self.BlockApplyWithDrawl) {
        self.BlockApplyWithDrawl(sender);
    }
}

@end
